// pages/conference/process_input.js
import {this_year,next_year,my_request,my_showToast_error,my_showToast_ok,gen_query_str} from '../../utils/my_utils'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    process_url:"/pages/conference/input_third",
    init_warning_danger:{
      content:"",
      start_time:"",
      end_time:""
    },
    submit_data_length:{},
    maxlength:{
      content:200,
    },
    must_fields:[
      {
        key:'content',
        value:"流程详情",
      },
      {
        key:"start_time",
        value:"开始时间"
      },
      {
        key:"end_time",
        value:"结束时间"
      }
    ],
    submit_data:{},
    typeChange:"change",
    typeAdd:"add",
    editType:"",
    default_start_time:"7:00",
    default_end_time:"23:59"
  },
  back_to_process:function(){
    wx.navigateBack();
  },
  save_and_go_back:function(){
    if(this.valid_submit_data()){
      if(this.data.editType==this.data.typeChange){
        if(this.check_update()){
          this.update_to_server()
        }
        else{
          this.back_to_process()
        }
      }
      else if(this.data.editType==this.data.typeAdd){
        this.insert_to_server()
      }
      else{
        my_showToast_error("逻辑错误41",2000)
      }
    }
  },
  input_text:function(e){
    var var_name = e.currentTarget.dataset.name;
    var max_length = +e.currentTarget.dataset.maxlength;
    var value = e.detail.value;
    var cursor = e.detail.cursor;
    var submit_data = this.data.submit_data;
    var submit_data_length = this.data.submit_data_length;
    submit_data[var_name] = value;
    submit_data_length[var_name]=value.length;
    this.setData({
      submit_data:submit_data,
      submit_data_length:submit_data_length
    })
  },
  timeChange:function(e){
    var key=e.currentTarget.dataset.type;
    var submit_data=this.data.submit_data;
    submit_data[key] = e.detail.value;
    this.setData({
      submit_data: submit_data
    })
  },
  save_props:function(options){
    var new_query_data={};
    for(var key in options){
      new_query_data[key]=decodeURI(options[key])
    }
    this.setData({
      query_data:new_query_data
    })
  },
  valid_submit_data:function(){
    var submit_data = this.data.submit_data;
    var must_fields = this.data.must_fields
    var key="";
    var desc="";
    for(var i=0;i<must_fields.length;++i){
      key = must_fields[i].key;
      desc= must_fields[i].value;
      if(!submit_data[key]){
        var warning_danger = {...this.data.init_warning_danger}
        //console.log(warning_danger)
        warning_danger[key]="danger"
        this.setData({
          warning_danger:warning_danger
        })
        my_showToast_error("输入错误",2000)
        return false;
      }
    }
    return true;
  },
  insert_to_server:function(){
    var that=this;
    var post_data={
      meeting:+this.data.query_data.edit_id,
      publisher:+this.data.query_data.publisher,
      time:this.data.submit_data.start_time+"-"+this.data.submit_data.end_time,
      content:this.data.submit_data.content
    }
    my_request({
      url: 'https://sichuangyi.demopp.in/api-conference-alpha/detail/add',
      method: "POST",
      data: post_data,
      success:function(res){
        my_showToast_ok("保存成功",2000)
        that.back_to_process()
      },
      fail: function() {
        my_showToast_error("保存失败",2000)
      }
    })
  },
  update_to_server:function(){
    var that=this;
    var post_data={
      meeting:+this.data.query_data.edit_id,
      publisher:+this.data.query_data.publisher,
      time:this.data.submit_data.start_time+"-"+this.data.submit_data.end_time,
      content:this.data.submit_data.content,
      process_id:+this.data.query_data.process_id
    }
    console.log(post_data)
    my_request({
      url: 'https://sichuangyi.demopp.in/api-conference-alpha/detail/update',
      method: "POST",
      data: post_data,
      success:function(res){
        my_showToast_ok("保存成功",2000)
        that.back_to_process()
      },
      fail: function() {
        my_showToast_error("保存失败",2000)
      }
    })
  },
  delete_from_server:function(){
    var that=this;
    var post_data={
      meeting:+this.data.query_data.edit_id,
      publisher:+this.data.query_data.publisher,
      process_id:+this.data.query_data.process_id
    }
    my_request({
      url: 'https://sichuangyi.demopp.in/api-conference-alpha/detail/remove',
      method: "POST",
      data: post_data,
      success:function(res){
        my_showToast_ok("删除成功",2000)
        that.back_to_process()
      },
      fail: function() {
        my_showToast_error("删除失败",2000)
      }
    })
  },
  check_update:function(){
    var submit_data=this.data.submit_data;
    var init_submit_data=this.data.init_submit_data;
    for(var key in submit_data){
      if(submit_data[key]!=init_submit_data[key]){
        return true
      }
    }
    for(var key in init_submit_data){
      if(init_submit_data[key]!=submit_data[key]){
        return true
      }
    }
    return false
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.save_props(options)
    var query_data=this.data.query_data;
    //载入编辑模式
    var editType="";
    if(query_data.type=="new"){
      editType=this.data.typeAdd
      this.setData({
        editType:editType
      })
    }
    else{
      if(!query_data.process_id){
        my_showToast_error("逻辑错误",2000);
      }
      else{
        editType=this.data.typeChange;
        this.setData({
          editType:editType
        })
        //渲染submit_data
        var time_list=query_data.time.split("-");
        var submit_data={
          start_time:time_list[0],
          end_time:time_list[1],
          content:query_data.content
        }
        this.setData({
          submit_data:submit_data,
          init_submit_data:{...submit_data},
        })
      }
    }
    
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  }
})